#ifndef __OBJ_H__
#define __OBJ_H__

#include <vector>
#include <list>
#include <string>
#include "StelCore.hpp"

class OBJ
{
    public:
        OBJ( void );
        virtual ~OBJ( void );

        void load( const char* filename );
        void prepareStelModels( void );
        void draw( StelCore* core );

        struct Vertex {
            float x, y, z;
        };

        struct Texcoord {
            float u, v;
        };

        struct Ref {
            unsigned int v, t, n; // vertex, texture, normal index
            bool texture; // use texture?
            bool normal; // use normal?
        };

        typedef std::list<Ref> RefList;

        struct Face {
            RefList refs;
        };

        typedef std::list<Face> FaceList;

        struct Model {
            Model() : faces(), name(), material(), refCount(0) {}
            FaceList faces;
            std::string name;
            std::string material;
            unsigned int refCount;
        };

        struct StelModel {
            StelModel() : vertice(NULL), normalArray(NULL), texCoords(NULL) {}
            Vec3d* vertice;
            Vec3f* normalArray;
            Vec2f* texCoords;
            unsigned int size;
        };

    private:
        std::vector<std::string> splitStr(const std::string& line, char delim);
        float parseFloat(const std::string& str);
        unsigned int parseInt(const std::string& str);

        bool loaded;

        std::vector<Vertex> vertices;
        std::vector<Vertex> normals;
        std::vector<Texcoord> texcoords;

        typedef std::list<Model> ModelList;

        ModelList models;

        typedef std::list<StelModel> StelModelList;

        StelModelList stelModels;
};

#endif

